﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;
using Facade;
using System.Collections;
using IronOne.SalesOne.DataObjects;

namespace IronOne.SalesOne.SalesOneWeb.UserManagement
{
    public partial class ViewUser : SOSecurityPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                ValidateUserCredentials(Request.Url.LocalPath);

                if (!IsPostBack)
                {
                    if (!string.IsNullOrEmpty(Request.UrlReferrer.ToString()))
                    {
                        ViewState["refUrl"] = Request.UrlReferrer.ToString();
                    }

                }

                //DropDownList idType = (DropDownList)wizCreateUser.CreateUserStep.ContentTemplateContainer.FindControl("drpIdType");
                CodeFacade coFac = new CodeFacade();
                List<IDType> id = coFac.GetIDTypes("CodeId ASC", 1);
                IDType idSel = new IDType();

                idSel.CodeId = -1;
                idSel.Description = "Select";
                id.Insert(0, idSel);

                drpIdType.DataSource = id;
                drpIdType.DataTextField = "Description";
                drpIdType.DataValueField = "CodeId";
                drpIdType.DataBind();

                //DropDownList country = (DropDownList)wizCreateUser.CreateUserStep.ContentTemplateContainer.FindControl("drpCountry");
                List<Country> coun = coFac.GetCountries("CodeId ASC", 1);
                Country cSel = new Country();
                cSel.CodeId = -1;
                cSel.Description = "Select";
                coun.Insert(0, cSel);

                drpCountry.DataSource = coun;
                drpCountry.DataTextField = "Description";
                drpCountry.DataValueField = "CodeId";
                drpCountry.DataBind();

                if (!IsPostBack)
                {
                    this.View();
                }
            }
            catch (Exception)
            {

            }
            
            
        }

        public void View()
        {
            try
            {
                string user = Session["userName"].ToString();
                MembershipUser usr = Membership.GetUser(user);
                Guid userGuuid = (Guid)usr.ProviderUserKey;

                StructureFacade StructureFacade = new StructureFacade();

                User usrObj = StructureFacade.GetUser(userGuuid);


                lblUserName.Text = usr.UserName;

                LinqUser lu = new LinqUser();
                string temp = lu.IsLinkedUserCheck(usr.UserName.ToLower());
                
                if(temp != null)
                {
                    lblIntranetUsername.Text = temp;
                }
                else
                {
                    lblIntranetUsername.Text = "User account not linked yet";
                }
                

                lblName.Text = usrObj.Name;
                lblIdNo.Text = usrObj.IdNo;
                lblAddress1.Text = usrObj.Address1;
                lblAddress2.Text = usrObj.Address2;
                lblCity.Text = usrObj.City;
                lblContactNo.Text = usrObj.Contact;
                lblPostalCode.Text = usrObj.PostalCode;
                lblEmail.Text = usr.Email;
                //lblSelectQ.Text = usr.PasswordQuestion;
                drpIdType.SelectedItem.Text = usrObj.IdType;
                drpCountry.SelectedItem.Text = usrObj.Country;
                drpGender.SelectedValue = usrObj.Gender;

                //roles
                StructureFacade facade = new StructureFacade();


                UserInformation info = facade.GetUserInformation(usr.UserName);

                List<UserType> userTypes = facade.GetUserTypes("UserTypeId ASC");

                rdoUserTypes.DataSource = userTypes;
                rdoUserTypes.DataTextField = "UserTypeName";
                rdoUserTypes.DataValueField = "UserTypeId";
                rdoUserTypes.DataBind();



                rdoUserTypes.Items[info.UserType - 1].Selected = true;


                switch (info.UserType)
                {
                    case 1:
                        //lblUserType.Text = "HQ";
                        Node.Visible = false;
                        break;
                    case 2:
                        //lblUserType.Text = "Group";
                        SalesOneGroup grt = facade.GetGroup((int)info.GroupId);
                        ListItem itg = new ListItem(grt.Name, info.GroupId.ToString());
                        drpGroup.Items.Insert(0, itg);


                        SalesOneGroup gr = facade.GetGroup(info.NodeId);
                        //lblUserNode.Text = gr.Name;
                        Session["groupId"] = info.GroupId;
                        break;
                    case 3:
                        //lblUserType.Text = "Branch";
                        Branch br = facade.GetBranch(info.NodeId);

                        SalesOneGroup gr1 = facade.GetGroup((int)info.GroupId);
                        ListItem itg1 = new ListItem(gr1.Name, info.GroupId.ToString());
                        drpGroup.Items.Insert(0, itg1);
                        ListItem itb1 = new ListItem(info.BranchName, info.BranchId.ToString());
                        drpBranch.Items.Insert(0, itb1);
                        Session["branchId"] = info.BranchId;
                        //lblUserNode.Text = gr1.LastUpdated +" -> "+  br.BranchName;
                        break;
                    case 4:
                        //lblUserType.Text = "Unit";
                        SalesOneUnit un = facade.GetUnit(info.NodeId);

                        SalesOneGroup grt1 = facade.GetGroup((int)info.GroupId);
                        ListItem itg2 = new ListItem(grt1.Name, info.GroupId.ToString());
                        drpGroup.Items.Insert(0, itg2);
                        ListItem itb2 = new ListItem(info.BranchName, info.BranchId.ToString());
                        drpBranch.Items.Insert(0, itb2);
                        ListItem itu1 = new ListItem(info.UnitName, info.UnitId.ToString());
                        drpUnit.Items.Insert(0, itu1);
                        Branch brs = facade.GetBranch((int)info.BranchId);
                        SalesOneGroup grs = facade.GetGroup((int)info.GroupId);
                        Session["unitId"] = info.UnitId;
                        //lblUserNode.Text = grs.Name + " -> " +  brs.BranchName + " -> " + un.Name;
                        break;
                    default:
                        break;
                }


                string[] roles = Roles.GetRolesForUser(usr.UserName);
                blRoles.DataSource = roles;
                blRoles.DataBind();
                Session.Remove("userName");
            }
            catch (Exception)
            {

            }
            
            //info.UserType
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                string refr = ViewState["refUrl"].ToString();
                Response.Redirect(refr);
            }
            catch (Exception)
            {

            }
            


        }
    }
}
